package com.example.bee_work_3;

import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.example.bee_work_3.boradcast.MyBroadCastReceiver;
import com.example.bee_work_3.fragment.MyFragmentAdapter;
import com.example.bee_work_3.fragment.home.Fragment_home;
import com.example.bee_work_3.fragment.project.Fragment_Project;
import com.example.bee_work_3.fragment.system.Fragment_System;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private MyBroadCastReceiver myBroadCastReceiver;
    private int moveOne;
    static int offsetx = 0, position = 0;

    private TextView home_text, system_text, project_text;
    public static SwipeLayout swipeLayout;
    private ImageView line;
    private ViewPager viewPager;

    private MyFragmentAdapter myFragmentAdapter;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        //网络变化广播
        myBroadCastReceiver = new MyBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(myBroadCastReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //切记注销
        unregisterReceiver(myBroadCastReceiver);
    }

    @TargetApi(Build.VERSION_CODES.M)
    public void init() {
        home_text = findViewById(R.id.home_text);
        system_text = findViewById(R.id.system_text);
        project_text = findViewById(R.id.project_text);
        viewPager = findViewById(R.id.viewPager);
        line = findViewById(R.id.line);
        initLine();
        swipeLayout = (SwipeLayout) findViewById(R.id.swipe_layout);
        RelativeLayout home_layout = (RelativeLayout) findViewById(R.id.home_layout);
        RelativeLayout system_layout = (RelativeLayout) findViewById(R.id.system_layout);
        RelativeLayout project_layout = (RelativeLayout) findViewById(R.id.project_layout);
        home_layout.setOnClickListener(this);
        system_layout.setOnClickListener(this);
        project_layout.setOnClickListener(this);

        Fragment_home fragment_home = new Fragment_home(getApplicationContext());
        Fragment_System fragment_system = new Fragment_System();
        Fragment_Project fragment_project = new Fragment_Project();
        fragmentList.add(fragment_home);
        fragmentList.add(fragment_system);
        fragmentList.add(fragment_project);
        myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(myFragmentAdapter);
        changeTab(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                offsetx = i1;
                moveLine(i, v, 150);
            }

            @Override
            public void onPageSelected(int i) {
                position = i;
                //swipeLayout.setSwipeEnabled(false);
                if (i == 0) {
                 swipeLayout.setSwipeEnabled(true);
                 } else {
                 swipeLayout.setSwipeEnabled(false);
                 }
                changeTab(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }

        });
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, final MotionEvent event) {
                if (offsetx == 0 && position == 0) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    swipeLayout.setSwipeEnabled(true);
                                    swipeLayout.onTouchEvent(event);
                                }
                            });
                        }
                    }).start();
                    return true;
                }
                return false;
            }
        });

    }

    public void initLine() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        ViewGroup.LayoutParams lp = line.getLayoutParams();
        lp.width = screenW / 3;
        line.setLayoutParams(lp);
        moveOne = lp.width;
    }

    public void moveLine(int position, float offset, int ms) {
        float curTranslationX = line.getTranslationX();
        float toPositionX = moveOne * position + offset;
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(line, "translationX", curTranslationX, toPositionX);
        objectAnimator.setDuration(ms);
        objectAnimator.start();
    }

    public void changeTab(int position) {
        for (int i = 0; i < 3; i++) {
            if (i != position) {
                changeColor(i, Color.BLACK);
            } else {
                changeColor(i, Color.RED);
            }
        }
    }

    public void changeColor(int position, int color) {
        switch (position) {
            case 0:
                home_text.setTextColor(color);
                break;
            case 1:
                system_text.setTextColor(color);
                break;
            case 2:
                project_text.setTextColor(color);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_layout:
                viewPager.setCurrentItem(0);
                break;
            case R.id.system_layout:
                viewPager.setCurrentItem(1);
                break;
            case R.id.project_layout:
                viewPager.setCurrentItem(2);
                break;
        }
    }
}